<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    /**
     * @var Zend_Log
     */
    protected $_logger;

    /**
     * @var Zend_Application_Module_Autoloader
     */
    protected $_resourceLoader;

    /**
     * Configure the pluginloader cache
     */
    protected function _initPluginLoaderCache()
    {
        if ('production' == $this->getEnvironment()) {
            $classFileIncCache = APPLICATION_PATH . '/../data/cache/pluginLoaderCache.php';
            if (file_exists($classFileIncCache)) {
                include_once $classFileIncCache;
            }
            Zend_Loader_PluginLoader::setIncludeFileCache($classFileIncCache);
        }
        
        //Zend_Session::start();
    }
  
    /**
     * Setup the logging
     */
    protected function _initLogging()
    {
        $this->bootstrap('frontController');
        $logger = new Zend_Log();
        
        $writer = 'production' == $this->getEnvironment() ?
			new Zend_Log_Writer_Stream(APPLICATION_PATH . '/../data/logs/app.log') :
			new Zend_Log_Writer_Firebug();
        $logger->addWriter($writer);

	if ('production' == $this->getEnvironment()) {
            $filter = new Zend_Log_Filter_Priority(Zend_Log::CRIT);
            $logger->addFilter($filter);
        }

        $this->_logger = $logger;
        Zend_Registry::set('log', $logger);
    }

    /**
     * Setup locale
     */
    protected function _initLocale()
    {
        $this->_logger->info('Bootstrap ' . __METHOD__);

        $locale = new Zend_Locale('en_GB');
        Zend_Registry::set('Zend_Locale', $locale);

        //setup application version
        Zend_Registry::set('APPLICATION_VERSION', $this->getOption("APPLICATION_VERSION"));
    }


    /**
     * Configure the default modules autoloading, here we first create
     * a new module autoloader specifiying the base path and namespace
     * for our default module. This will automatically add the default
     * resource types for us. We also add two custom resources for Services
     * and Model Resources.
     */
    protected function _initDefaultModuleAutoloader()
    {
        $this->_logger->info('Bootstrap ' . __METHOD__);

        $this->_resourceLoader = new Zend_Application_Module_Autoloader(array(
            'namespace' => 'Cms',
            'basePath'  => APPLICATION_PATH . '/modules/cms'
        ));
        $this->_resourceLoader->addResourceTypes(array(
                'modelResource' => array(
                'path' => 'models/resources',
                'namespace' => 'Resource'
            ),
                'service' => array(
                'path' => 'services',
                'namespace' => 'Service'
            )
            ));
    }


    /**
     * Setup the database profiling
     */
    protected function _initDbProfiler()
    {
        $this->_logger->info('Bootstrap ' . __METHOD__);

        $this->bootstrap('db');
        $dbAdapter = $this->getPluginResource('db')->getDbAdapter();
        
        if ('production' !== $this->getEnvironment()) {
            
            $profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
            $profiler->setEnabled(true);
            $dbAdapter->setProfiler($profiler);
        }
        
        Zend_Registry::set('db', $dbAdapter);
    }

    /**
     * Setup the view
     */
    protected function _initViewSettings()
    {
        $this->bootstrap('View');
        $view = $this->getResource('View');

        // set encoding and doctype
        $view->setEncoding('UTF-8');
        $view->doctype('XHTML1_STRICT');

        // set the content type and language
        $view->headMeta()->appendHttpEquiv('Content-Type', 'text/html; charset=UTF-8');
        $view->headMeta()->appendHttpEquiv('Content-Language', 'en-US');

        // Set the initial title and separator:
        $view->headTitle('PacePluX-BIS')
             ->setSeparator(' :: ');

        // Set the initial stylesheet:
        $view->headLink()->prependStylesheet('css/stylesheet.css');
        $view->headLink()->appendStylesheet('js/jquery/ui/themes/ui-lightness/jquery.ui.all.css');

        // Set the initial JS to load:
        $view->headScript()->prependFile('js/jquery/jquery-1.7.2.js');
        $view->headScript()->appendFile('js/jquery/ui/jquery.ui.core.js');
        $view->headScript()->appendFile('js/jquery/superfish/js/superfish.js');

        //set up the Navigation service
        $cmsNavigationConfig = new Zend_Config_Xml(APPLICATION_PATH.'/configs/cmsNavigation.xml', 'nav');
        $navContainer = new Zend_Navigation($cmsNavigationConfig);
        $view->navigation($navContainer);
        
    }

    /**
     * Add required routes to the router
     */
     protected function _initRouting()
    {
        $this->bootstrap('frontController');
        $router = $this->frontController->getRouter();
        $route = new Zend_Config_Ini(APPLICATION_PATH.'/configs/cmsRouter.ini','production');
        $router->addConfig($route, 'routes');
    }


    /**
     * Add Controller Action Helpers
     */
    protected function _initActionHelpers()
    {
        $this->_logger->info('Bootstrap ' . __METHOD__);
        Zend_Controller_Action_HelperBroker::addPrefix('BIS_Controller_Helper');
    }

    
    /**
     * Init the db metadata and paginator caches
     
    protected function _initDbCaches()
    {
        $this->_logger->info('Bootstrap ' . __METHOD__);
        if ('production' == $this->getEnvironment()) {
            // Metadata cache for Zend_Db_Table
            $frontendOptions = array(
                'automatic_serialization' => true
            );

            $cache = Zend_Cache::factory('Core',
                'Apc',
                $frontendOptions
            );
            Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
        }
    }
    */      
    
    /**
     * Init the db metadata and paginator caches
     */
    protected function _initDbCaches()
    {
    	$this->_logger->info('Bootstrap ' . __METHOD__);
    
    	if ('production' == $this->getEnvironment()) {
    		 
    		// Metadata cache for Zend_Db_Table
    		$frontendOptions = array(
    				'automatic_serialization' => true
    		);
    
    		$backendOptions = array(
    				'cache_dir'=> APPLICATION_PATH . '/../data/cache/db'
    		);
    
    		$cache = Zend_Cache::factory(
    				'Core',
    				'File',
    				$frontendOptions,
    				$backendOptions
    		);
    
    		Zend_Db_Table_Abstract::setDefaultMetadataCache($cache);
    
    	}
    }
}